package com.zabra.zl.ui.free_interset;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xujiaji.happybubble.BubbleDialog;
import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.ui.free_interset.contract.FreeIntersetContract;
import com.zabra.zl.ui.free_interset.presenter.FreeIntersetPresenter;
import com.zabra.zl.ui.success.AllocationSuccessActivity;
import com.zabra.zl.utils.BaseUtils;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

/**
 * 免息配资
 */
public class FreeintersetActivity extends BaseMvpActivity<FreeIntersetPresenter> implements FreeIntersetContract.View {
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.cashdeposit)
    TextView cashdeposit;
    @BindView(R.id.interset)
    TextView interset;
    @BindView(R.id.totlemoney)
    TextView totlemoney;
    @BindView(R.id.text1)
    TextView text1;
    @BindView(R.id.text2)
    TextView text2;
    @BindView(R.id.iv_warning)
    ImageView ivWarning;
    @BindView(R.id.text3)
    TextView text3;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.text4)
    TextView text4;
    @BindView(R.id.text5)
    TextView text5;
    @BindView(R.id.text6)
    TextView text6;
    @BindView(R.id.cb_agree)
    CheckBox cbAgree;
    @BindView(R.id.look_agree)
    TextView lookAgree;
    @BindView(R.id.tv_apply)
    TextView tvApply;
    private int mIndex;
    private Bundle bundle;
    private Double strLilv, strBaozheng, strPeizi;
    private int timeType, bpid, bei, bpAround;
    private String baozheng, lilv, peizi;
    private Dialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_freeinterset;
    }

    @Override
    public void initView() {
        bundle = getIntent().getExtras();
        mIndex = bundle.getInt("mIndex");
        timeType = bundle.getInt("timetype");
        bpid = bundle.getInt("bpid");
        bei = bundle.getInt("bei");
        baozheng = bundle.getString("baozheng");
        lilv = bundle.getString("lilv");
        peizi = bundle.getString("peizi");

        strBaozheng = Double.parseDouble(baozheng);
        strLilv = Double.parseDouble(lilv);
        strPeizi = Double.parseDouble(peizi);
        //保证金
        cashdeposit.setText(baozheng);
        //利息
        double lixi = strPeizi * strLilv / 100;
        interset.setText(BaseUtils.bit1(lixi));
        //准备资金
        double zhunbei = strBaozheng + lixi;
        totlemoney.setText(BaseUtils.bit1(zhunbei));
        //配资资金
        text1.setText(peizi);
        //操盘总额
        text2.setText((strPeizi + strBaozheng) + "");
        //预警线=配资资金+保证金×0.5%
        text3.setText((((strBaozheng * 0.5) / 100) + strPeizi) + "");
        //平仓线=配资资金+保证金×0.3%
        text4.setText((((strBaozheng * 0.3) / 100) + strPeizi) + "");
        //资金日利率
        text6.setText(lilv + "%");

        switch (mIndex) {
            case Config
                    .FREE:
                tvTitle.setText("免息配资");
                bpAround = 2;
                break;
            case Config
                    .DAY:
                tvTitle.setText("按日配资");
                bpAround = 2;
                break;
            case Config
                    .MON:
                tvTitle.setText("按月配资");
                bpAround = 1;
                break;
        }
        mPresenter = new FreeIntersetPresenter();
        mPresenter.attachView(this);

    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            toClass1(this, AllocationSuccessActivity.class);
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }

    @OnCheckedChanged({R.id.cb_agree})
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_agree:
                if (isChecked) {
                    tvApply.setEnabled(true);
                    tvApply.setAlpha(1);
                } else {
                    tvApply.setEnabled(false);
                    tvApply.setAlpha(0.5f);
                }
                break;
        }


    }

    @OnClick({R.id.back, R.id.iv_warning, R.id.iv_close, R.id.look_agree, R.id.tv_apply})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.iv_warning:
                new BubbleDialog(this)
                        .addContentView(LayoutInflater.from(this).inflate(R.layout.boom_warm, null))
                        .setClickedView(ivWarning)
                        .setOffsetY(-15)
                        .show();
                break;
            case R.id.iv_close:
                new BubbleDialog(this)
                        .addContentView(LayoutInflater.from(this).inflate(R.layout.boom_close, null))
                        .setClickedView(ivClose)
                        .setOffsetY(-15)
                        .show();
                break;
            case R.id.look_agree:
                break;
            case R.id.tv_apply:
                mPresenter.getData(baozheng, bpid, bei, mIndex, lilv, bpAround, timeType);
                break;
        }
    }
}
